/**
 * This file is part of the DbUnit module for NetBeans IDE
 * Copyright (C) 2009 Pavol Pitoňák
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

package cz.muni.fi.dbunit;

import java.awt.Component;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.prefs.Preferences;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.netbeans.api.project.Project;
import org.netbeans.modules.junit.JUnitSettings;
import org.openide.WizardDescriptor;
import org.openide.util.HelpCtx;
import org.openide.util.NbPreferences;

/**
 *
 * @author Pavol Pitoňák <pavol@pitonak.com>
 */
public class SimpleTestWizardPanel implements WizardDescriptor.Panel, DocumentListener {

    /**
     * The visual component that displays this panel. If you need to access the
     * component from this class, just use getComponent().
     */
    private SimpleTestVisualPanel component;
    private Project project;
    private boolean isValid;
    private String className;
    private final Set<ChangeListener> listeners = new HashSet<ChangeListener>(1); // or can use ChangeSupport in NB 6.0

    /**
     *
     * @param project
     * @param className "" or class name if it is a panel for 1 class,
     *        null if more classes have been selected
     */
    public SimpleTestWizardPanel(Project project, String className) {
        this.project = project;
        this.className = className;
    }

    // Get the visual component for the panel. In this template, the component
    // is kept separate. This can be more efficient: if the wizard is created
    // but never displayed, or not all panels are displayed, it is better to
    // create only those which really need to be visible.
    @Override
    public Component getComponent() {
        if (component == null) {
            component = new SimpleTestVisualPanel(project, className);
            component.getClassToTestTextField().getDocument().addDocumentListener(this);
            if (className == null || !"".equals(className)) {
                setValid(true);
            } else {
                setValid(false);
            }
        }
        return component;
    }

    @Override
    public HelpCtx getHelp() {
        // Show no Help button for this panel:
        return HelpCtx.DEFAULT_HELP;
    // If you have context help:
    // return new HelpCtx(SampleWizardPanel1.class);
    }

    @Override
    public boolean isValid() {
        // If it is always OK to press Next or Finish, then:
        //return true;
        // If it depends on some condition (form filled out...), then:
        //return component.isValideClass();
        // and when this condition changes (last form field filled in...) then:
        // fireChangeEvent();
        // and uncomment the complicated stuff below.
        return isValid;
    }

    @Override
    public final void addChangeListener(ChangeListener l) {
        synchronized (listeners) {
            listeners.add(l);
        }
    }

    @Override
    public final void removeChangeListener(ChangeListener l) {
        synchronized (listeners) {
            listeners.remove(l);
        }
    }

    protected final void fireChangeEvent() {
        Iterator<ChangeListener> it;
        synchronized (listeners) {
            it = new HashSet<ChangeListener>(listeners).iterator();
        }
        ChangeEvent ev = new ChangeEvent(this);
        while (it.hasNext()) {
            it.next().stateChanged(ev);
        }
    }

    // You can use a settings object to keep track of state. Normally the
    // settings object will be the WizardDescriptor, so you can use
    // WizardDescriptor.getProperty & putProperty to store information entered
    // by the user.
    @Override
    public void readSettings(Object settings) {
        Preferences pref = NbPreferences.forModule(SimpleTestWizardPanel.class);

        component.setPublicMethods(pref.getBoolean("stw_publicMethods", true));
        component.setProtectedMethods(pref.getBoolean("stw_protectedMethods", true));
        component.setPackagePrivateMethods(pref.getBoolean("stw_packagePrivateMethods", true));
        component.setTestInitializer(pref.getBoolean("stw_testInitializer", true));
        component.setTestFinalizer(pref.getBoolean("stw_testFinalizer", true));
        component.setMethodBodies(pref.getBoolean("stw_defaultMethodBodies", true));
        component.setJavadocComments(pref.getBoolean("stw_javadocComments", true));
        component.setSourceCodeHints(pref.getBoolean("stw_sourceCodeHints", true));
    }

    @Override
    public void storeSettings(Object settings) {
        // in the case we press the 'Back' button before choosing a class
        if (component.getClassName().isEmpty()) {
            return;
        }

        JUnitSettings jUnitSettings = JUnitSettings.getDefault();

        ((WizardDescriptor) settings).putProperty(SimpleTestVisualPanel.CLASS_TO_TEST,
                component.getClassName());

        ((WizardDescriptor) settings).putProperty(SimpleTestVisualPanel.PACKAGE,
                component.getPackage());

        ((WizardDescriptor) settings).putProperty(SimpleTestVisualPanel.PUBLIC_METHODS,
                component.isPublicMethods());
        ((WizardDescriptor) settings).putProperty(SimpleTestVisualPanel.PROTECTED_METHODS,
                component.isProtectedMethods());
        ((WizardDescriptor) settings).putProperty(SimpleTestVisualPanel.PACKAGE_PRIVATE,
                component.isPackagePrivateMethods());

        ((WizardDescriptor) settings).putProperty(SimpleTestVisualPanel.TEST_INITIALIZER,
                component.isTestInitializer());
        ((WizardDescriptor) settings).putProperty(SimpleTestVisualPanel.TEST_FINALIZER,
                component.isTestFinalizer());
        ((WizardDescriptor) settings).putProperty(SimpleTestVisualPanel.DEFAULT_METHOD_BODIES,
                component.isDefaultMethodBodies());

        ((WizardDescriptor) settings).putProperty(SimpleTestVisualPanel.GENERATE_JAVADOC,
                component.isJavadocComments());
        ((WizardDescriptor) settings).putProperty(SimpleTestVisualPanel.GENERATE_HINTS,
                component.isSourceCodeHints());

        jUnitSettings.setMembersPublic(component.isPublicMethods());
        jUnitSettings.setMembersProtected(component.isProtectedMethods());
        jUnitSettings.setMembersPackage(component.isPackagePrivateMethods());
        jUnitSettings.setGenerateSetUp(component.isTestInitializer());
        jUnitSettings.setGenerateTearDown(component.isTestFinalizer());
        jUnitSettings.setBodyContent(component.isDefaultMethodBodies());
        jUnitSettings.setJavaDoc(component.isJavadocComments());
        jUnitSettings.setBodyComments(component.isSourceCodeHints());

        Preferences pref = NbPreferences.forModule(SimpleTestWizardPanel.class);

        pref.putBoolean("stw_publicMethods", component.isPublicMethods());
        pref.putBoolean("stw_protectedMethods", component.isProtectedMethods());
        pref.putBoolean("stw_packagePrivateMethods", component.isPackagePrivateMethods());
        pref.putBoolean("stw_testInitializer", component.isTestInitializer());
        pref.putBoolean("stw_testFinalizer", component.isTestFinalizer());
        pref.putBoolean("stw_defaultMethodBodies", component.isDefaultMethodBodies());
        pref.putBoolean("stw_javadocComments", component.isJavadocComments());
        pref.putBoolean("stw_sourceCodeHints", component.isSourceCodeHints());
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        change();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        change();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        change();
    }

    private void change() {
        if (!component.isValideClass()) {
            setValid(true);
        } else {
            setValid(false);
        }
    }

    private void setValid(boolean val) {
        if (isValid != val) {
            isValid = val;
            fireChangeEvent();  // must do this to enable next/finish button
        }
    }
}

